package com.taplinker.client.im.activity.message;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;
import com.taplinker.client.AppConfigContext;
import com.taplinker.client.R;
import com.taplinker.client.im.activity.PictureDetailActivity;
import com.taplinker.client.im.protocol.TapLinkerMessage;
import com.taplinker.client.widget.image.TaplinkerImageView;
import com.taplinker.core.util.ImageUtil;
import com.taplinker.core.util.TimeUtil;

public class RightImageMessage extends AbstractMessageView {

	@Override
	public View getMessageView(final Context context, TapLinkerMessage message, View convertView) {
		final ImageMessageHolder imageMessageHolder;
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(MessageTypeEnum.getMessageType(message).getLayoutId(), null);
			imageMessageHolder = new ImageMessageHolder();
			imageMessageHolder.name = (TextView) convertView.findViewById(R.id.name_text_tv);
			imageMessageHolder.head = (SmartImageView) convertView.findViewById(R.id.head_icon_iv);
			imageMessageHolder.image = (TaplinkerImageView) convertView.findViewById(R.id.content_img_tv);
			imageMessageHolder.time = (TextView) convertView.findViewById(R.id.time_text_tv);
			imageMessageHolder.state  = (TextView) convertView.findViewById(R.id.sender_state_tv);
			convertView.setTag(imageMessageHolder);
		} else {
			imageMessageHolder = (ImageMessageHolder) convertView.getTag();
		}
//		if(!TextUtils.isEmpty(message.getIcon())) {
			String headUrl = AppConfigContext.WEB_DISK_ROOT + message.getIcon();
//			ImageManager.getInstance().loadImage(headUrl, imageMessageHolder.head, ImageUtil.dip2px(context, 45), ImageUtil.dip2px(context, 45),null);
			
			// 取出自己的头像
//			SharedPreferences sp = context.getSharedPreferences(Base1Activity.SP_FILE_NAME, Context.MODE_APPEND);
//			headUrl = sp.getString(Base1Activity.USER_IMAGE_URL, null);
//			
//			imageMessageHolder.name.setText(sp.getString(Base1Activity.USER_NICK_NAME, ""));
			
			if( headUrl!=null && !"".equals(headUrl.trim()) ) {
				imageMessageHolder.head.setImageUrl(headUrl);
			}
//			Log.v("log_ktian", "我的头像IAMGE:"+headUrl);
//		}
		final String path = getAccessUrl(message);
		
		
		imageMessageHolder.image.setUrl(path);
		imageMessageHolder.image.setClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String url = path;
//				try {
//					if(url.contains("http://")) {
//	    				LogUtil.d("http", "get from remote");
//	    				HttpCacheItem item = HttpCacheDAO.getInstance().getFileIndexItem(CacheKey.getCacheKey(new URL(url).getPath()));
//	        			if(item != null && !item.isTemp())
//	        				url = item.getPath();
//	        			else
//	        				return;
//	    			}
//				} catch (MalformedURLException e) {
//					e.printStackTrace();
//				}
				Intent intent = new Intent(context, PictureDetailActivity.class);
				intent.putExtra("FilePath", url);
				context.startActivity(intent);
				
			}
		});
//		ImageManager.getInstance().loadImage(path, imageMessageHolder.image, 160, 120, new ImageManager.LocalCallBack() {
//			
//			@Override
//			public void callBack(final Object obj) {
//				imageMessageHolder.image.setOnClickListener(new View.OnClickListener() {
//					
//					@Override
//					public void onClick(View v) {
//						String url = path;
//						try {
//							if(url.contains("http://")) {
//			    				LogUtil.d("http", "get from remote");
//			    				HttpCacheItem item;
//								item = HttpCacheDAO.getInstance().getFileIndexItem(CacheKey.getCacheKey(new URL(url).getPath()));
//			        			if(item != null&&!item.isTemp())
//			        				url = item.getPath();
//			        			else
//			        				return;
//			    			}
//						} catch (MalformedURLException e) {
//							e.printStackTrace();
//						}
//						Intent intent = new Intent(context, PictureDetailActivity.class);
//						IntentTempData.getInstance().setImgPath(url);
//						IntentTempData.getInstance().setPickPic(false);
//						context.startActivity(intent);
//					}
//				});
//			}
//
//			@Override
//			public void updateProgres(long planSize, long curSize) {
//
//			}
//			
//			@Override
//			public void no() {
//				
//			}
//			
//		});
		
		imageMessageHolder.state.setText("");
		imageMessageHolder.state.setPadding(
				ImageUtil.dip2px(context, 5),
				ImageUtil.dip2px(context, 3),
				ImageUtil.dip2px(context, 5),
				ImageUtil.dip2px(context, 3));
		imageMessageHolder.state.setBackgroundResource(R.drawable.message_state_bg);
		if (message.isUnarrived()){
			// 切换未送到消息图标提示
			imageMessageHolder.state.setTextColor(Color.RED);
			imageMessageHolder.state.setPadding(0, 0, 0, 0);
			imageMessageHolder.state.setBackgroundResource(R.drawable.msg_warning);
		} else if (message.getSendTime()==null) {
		//	imageMessageHolder.state.setText("发送中");
		//	imageMessageHolder.state.setTextColor(0xff4dae4b);
		} else if (message.getRead()) {
		//	imageMessageHolder.state.setText("已读");
			imageMessageHolder.state.setBackgroundResource(R.drawable.input_read);
			//imageMessageHolder.state.setTextColor(0xff4dae4b);
		} else {
//			imageMessageHolder.state.setText("已送达");
//			imageMessageHolder.state.setTextColor(0xffff8a00);
			imageMessageHolder.state.setBackgroundResource(R.drawable.input_songda);
		}
		if(message.getSendTime() != null) {
			imageMessageHolder.time.setText(TimeUtil.timeFormat(message.getSendTime()));
		}
		return convertView;
	}
	
	class ImageMessageHolder {
		TextView name;		// 用户名
		SmartImageView head;		// 头像
		TaplinkerImageView image;	// 图片
		TextView time;		// 时间
		TextView state;		// 发送状态
	}

}
